package nccloud.sso.dto;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import nc.impl.pubapp.pattern.database.DataAccessUtils;
import nc.vo.pubapp.pattern.data.IRowSet;
import nccloud.framework.core.exception.BusinessException;

/**
 * 单点登录参数类
 */
public class SSOParamDTO implements Serializable{

	/**
	 * chenjma
	 */
	private static final long serialVersionUID = 1L;

	//********系统参数*************//
	private String datasource;//数据源sysconfig.bat中可以看到
	private String thirdsys_client_id;//第三方系统id sm_oauth_security表中的client_id
	private String thirdsys_client_security;//第三方秘钥sm_oauth_security表中的client_security2
	private String langcode;//多语语种
	private String busicentercode;//账套编码
	private String req_token_address;
	private String ip;//ncc ip
	private Integer port;//ncc port //在请求token是port已经写死记得修改

	//***********固定常量***********//
	private String appcode = "10160501";//审批中心详情应用编码
	private String pagecode = "10160501APPROVE";//审批中心详情页面编码

	//**********变量(外部传入)***********//
	private String usercode;//NCC的用户编码
	private String openType; //1 审批详情页面 2单据详情页面 3单据新增页面 4首页
	private String pk_message;
	private String pageMsgType;
	private String pk_bill;
	private String c; //被审批单据的应用编码
	private String p; //被审批单据的页面编码
	private String tradetype;//交易类型

	private int srcType; //来源类型  钉钉 1 OA 2  登录 3

	//竹云接口配置参数
	private String oauthServerUrl;//竹云服务地址
	private String oauthClientId;//竹云注册第三方应用id
	private String oauthClientSecret;//竹云注册第三方应用密钥
	private String oauthEsbServerUrl;//竹云ESB服务地址

	public int getSrcType() {
		return srcType;
	}

	public void setSrcType(int srcType) {
		this.srcType = srcType;
	}

	public String getDatasource() {
		return datasource;
	}

	public void setDatasource(String datasource) {
		this.datasource = datasource;
	}

	public String getUsercode() {
		return usercode;
	}

	public void setUsercode(String usercode) {
		this.usercode = usercode;
	}

	public String getThirdsys_client_id() {
		return thirdsys_client_id;
	}

	public void setThirdsys_client_id(String thirdsys_client_id) {
		this.thirdsys_client_id = thirdsys_client_id;
	}

	public String getThirdsys_client_security() {
		return thirdsys_client_security;
	}

	public void setThirdsys_client_security(String thirdsys_client_security) {
		this.thirdsys_client_security = thirdsys_client_security;
	}

	public String getLangcode() {
		return langcode;
	}

	public void setLangcode(String langcode) {
		this.langcode = langcode;
	}

	public String getBusicentercode() {
		return busicentercode;
	}

	public void setBusicentercode(String busicentercode) {
		this.busicentercode = busicentercode;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public String getOpenType() {
		return openType;
	}

	public void setOpenType(String openType) {
		this.openType = openType;
	}

	public String getPk_message() {
		return pk_message;
	}

	public void setPk_message(String pk_message) {
		this.pk_message = pk_message;
	}

	public String getPageMsgType() {
		return pageMsgType;
	}

	public void setPageMsgType(String pageMsgType) {
		this.pageMsgType = pageMsgType;
	}

	public String getC() {
		return c;
	}

	public void setC(String c) {
		this.c = c;
	}

	public String getP() {
		return p;
	}

	public void setP(String p) {
		this.p = p;
	}
	public String getAppcode() {
		return appcode;
	}

	public void setAppcode(String appcode) {
		this.appcode = appcode;
	}

	public String getPagecode() {
		return pagecode;
	}

	public void setPagecode(String pagecode) {
		this.pagecode = pagecode;
	}

	public String getReq_token_address() {
		return req_token_address;
	}

	public void setReq_token_address(String req_token_address) {
		this.req_token_address = req_token_address;
	}
	public String getPk_bill() {
		return pk_bill;
	}

	public void setPk_bill(String pk_bill) {
		this.pk_bill = pk_bill;
	}

	public String getTradetype() {
		return tradetype;
	}

	public void setTradetype(String tradetype) {
		this.tradetype = tradetype;
	}

	public String getOauthServerUrl() {
		return oauthServerUrl;
	}

	public void setOauthServerUrl(String oauthServerUrl) {
		this.oauthServerUrl = oauthServerUrl;
	}

	public String getOauthClientId() {
		return oauthClientId;
	}

	public void setOauthClientId(String oauthClientId) {
		this.oauthClientId = oauthClientId;
	}

	public String getOauthClientSecret() {
		return oauthClientSecret;
	}

	public void setOauthClientSecret(String oauthClientSecret) {
		this.oauthClientSecret = oauthClientSecret;
	}

	public String getOauthEsbServerUrl() {
		return oauthEsbServerUrl;
	}

	public void setOauthEsbServerUrl(String oauthEsbServerUrl) {
		this.oauthEsbServerUrl = oauthEsbServerUrl;
	}

	/**
	 * 加载系统参数
	 */
	@Deprecated
	public void loadSysParam() {
		this.datasource = "design";
		this.thirdsys_client_id = "scjcoa";
		this.thirdsys_client_security = "qfidufeiuvsvieaguaisfjfgaeu";
		this.langcode = "";
		this.busicentercode = "develop";
		this.ip = "127.0.0.1";
		this.port = 3006;
	}

	/**
	 * 转为审批地址参数
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String toApproveUrlParam() throws UnsupportedEncodingException {
		StringBuilder builder = new StringBuilder("");
		builder.append("/nccloud/resources/uap/msgcenter/message/approveDetail/index.html#/?")
		.append("pk_message=").append(this.pk_message)
		.append("&pageMsgType=").append(this.pageMsgType)
		.append("&appcode=").append(this.appcode)
		.append("&pagecode=").append(this.pagecode)
		.append("&c=").append(this.c)
		.append("&p=").append(this.p)
		.append("&srcType=").append(this.srcType);
		//需要对审批地址进行两次Url编码
		String firstResult = URLEncoder.encode(builder.toString(), "UTF-8");
		String result = URLEncoder.encode(firstResult, "UTF-8");
		return result;
	}

	/**
	 * 转为单据卡片浏览地址参数
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String toBillBrowseUrlParam() throws Exception {
		try {
			StringBuilder builder = new StringBuilder("");
			String [] array = getAppInfoByCode(this.appcode,this.pagecode);
			String pageUrl = array[3];
			String pk_appregister = array[0];
			if(pageUrl != null) {
				builder.append(pageUrl);
				builder.append("?appcode=").append(this.appcode);
				builder.append("&pagecode=").append(this.getPageCode());
				builder.append("&tradetype=").append(this.tradetype);
				builder.append("&id=").append(this.pk_bill);
				builder.append("&status=").append("browse");
				builder.append("&scene=").append("bz");

				String firstUrl = URLEncoder.encode(builder.toString(), "UTF-8");
				firstUrl=URLEncoder.encode(firstUrl, "UTF-8");
				String finalUrl = firstUrl + "&c=" + this.appcode;
				finalUrl=finalUrl + "&p=" + this.getPageCode();
				finalUrl=finalUrl + "&ar=" + pk_appregister;//应用注册主键

				//需要对审批地址进行两次Url编码
				//				String firstResult = URLEncoder.encode(builder.toString(), "UTF-8");
				//				String result = URLEncoder.encode(builder.toString(), "UTF-8");
				return finalUrl;
			}else {
				throw new BusinessException("根据传入页面编码【" + this.pagecode + "】未能查询到注册页面");
			}
		}catch(Exception e) {
			throw new BusinessException("生成表单【" + this.pagecode + "】详情地址异常！" + e.getMessage());
		}
	}

	/**
	 * 转为单据新增地址参数
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String toBillAddUrlParam() throws UnsupportedEncodingException {
		try {
			StringBuilder builder = new StringBuilder("");
			String [] array = getAppInfoByCode(this.appcode,this.pagecode);
			String pageUrl = array[3];
			String pk_appregister = array[0];
			if(pageUrl != null) {
				builder.append(pageUrl);
				builder.append("?appcode=").append(this.appcode);
				builder.append("&pagecode=").append(this.getPageCode());
				builder.append("&tradetype=").append(this.tradetype);
				builder.append("&status=").append("add");
				builder.append("&scene=").append("bz");

				String firstUrl = URLEncoder.encode(builder.toString(), "UTF-8");
				firstUrl=URLEncoder.encode(firstUrl, "UTF-8");
				String finalUrl = firstUrl + "&c=" + this.appcode;
				finalUrl=finalUrl + "&p=" + this.getPageCode();
				finalUrl=finalUrl + "&ar=" + pk_appregister;//应用注册主键

				//需要对审批地址进行两次Url编码
				//				String firstResult = URLEncoder.encode(builder.toString(), "UTF-8");
				//				String result = URLEncoder.encode(builder.toString(), "UTF-8");
				return finalUrl;
			}else {
				throw new BusinessException("根据传入页面编码【" + this.pagecode + "】未能查询到注册页面");
			}
		}catch(Exception e) {
			throw new BusinessException("生成表单【" + this.pagecode + "】详情地址异常！" + e.getMessage());
		}
	}

	/**
	 *  根据不同应用节点传值pagecode
	 * @return
	 */
	private String getPageCode() {
		String pagecode = this.pagecode;
		if("20080PBM_CARD".equals(this.pagecode)){//应付单
			if(this.tradetype!=null) {
				pagecode = this.tradetype;
			}
		}
		return pagecode;
	}

	/**
	 * 根据单据类型取到主表名称以及聚合VO的类型名称
	 * @param billtype
	 * @return
	 */
	public String [] getAppInfoByCode(String appcode,String pagecode){
		String sql = "select PARENT_ID,PARENTCODE,PAGECODE,PAGEURL from sm_apppage where PARENTCODE='" + appcode + "' and PAGECODE='" + pagecode + "'";
		DataAccessUtils dao = new DataAccessUtils();
		IRowSet rowset = dao.query(sql.toString());

		String[][] result = new String[1][4];
		if(rowset != null && rowset.size() > 0){
			result = rowset.toTwoDimensionStringArray();
		}
		return result[0];
	}

	@Override
	public String toString() {
		return "SSOParamDTO [datasource=" + datasource + ", thirdsys_client_id=" + thirdsys_client_id
				+ ", thirdsys_client_security=" + thirdsys_client_security + ", langcode=" + langcode
				+ ", busicentercode=" + busicentercode + ", req_token_address=" + req_token_address + ", ip=" + ip
				+ ", port=" + port + ", appcode=" + appcode + ", pagecode=" + pagecode + ", usercode=" + usercode
				+ ", openType=" + openType + ", pk_message=" + pk_message + ", pageMsgType=" + pageMsgType
				+ ", pk_bill=" + pk_bill + ", c=" + c + ", p=" + p + "]";
	}

}
